class HiringManagerController < ApplicationController
  def home
    uri = URI.parse('http://140.211.68.130/GetAllPositionTypes')
    resp = Net::HTTP.get_response(uri)
    data = resp.body
    result = JSON.parse(data)

    @positions = result['positions']
  end

  def request_openings
    all_positions_uri = URI.parse('http://140.211.68.130/GetAllPositionTypes')
    all_positions_response = Net::HTTP.get_response(all_positions_uri)
    all_positions_data = all_positions_response.body
    all_positions_result = JSON.parse(all_positions_data)
    @all_positions = all_positions_result['positions']

    @store_id = params[:store_id]
    open_positions_uri = URI.parse('http://140.211.68.130/GetOpenPositions')
    open_positions_response = Net::HTTP.post_form(open_positions_uri, store_id: @store_id)
    open_positions_data = open_positions_response.body
    open_positions_result = JSON.parse(open_positions_data)
    @open_positions = open_positions_result['positions']

    @positions = []
    @all_positions.each do |position|
      @position = Position.new
      @position.position_type_id = position['position_type_id'].to_s
      @position.title = position['title']
      @position.description = position['description']
      @position.store_id = @store_id
      @position.is_open = FALSE

      @open_positions.each do |open_position|
        if  position['position_type_id'] === open_position['position_type_id']
          @position.posting_id = open_position['posting_id'].to_s
          @position.city = open_position['city']
          @position.state = open_position['state']
          @position.is_open = TRUE
          @position.job_count = open_position['job_count']
        end
      end

      @positions.append(@position)
    end
  end

  def submit
    @posting_id = params[:posting_id]
    @position_type_id = params[:position_type_id]
    @title = params[:title]
    @description = params[:description]
    @store_id = params[:store_id]
    @is_open = params[:is_open]
    @job_count = params[:job_count]

    @position = Position.new(posting_id: @posting_id, position_type_id: @position_type_id, title: @title,
                             description: @description, store_id: @store_id, is_open: @is_open, job_count: @job_count)
    @position.save

    redirect_to hiring_manager_request_openings_path(store_id: 1)
  end

  def add_questions
    uri = URI.parse('http://140.211.68.130/GetAllPositionTypes')
    resp = Net::HTTP.get_response(uri)
    data = resp.body
    result = JSON.parse(data)

    @positions = result['positions']
  end

  def questions
    @position_type_id = ''
    posID = params[:id]

    params.each do |key,value|
      if key.to_s == 'id'
        @position_type_id = value
      end #end if
    end #end loop

    require 'net/http'
    source = URI.parse('http://140.211.68.130/GetQuestions')
    resp = Net::HTTP.post_form(source, 'position_type_id' => @position_type_id.to_s)
    data = resp.body.to_s

    begin
      result = JSON.parse(data)
      @full = result['questions']
    rescue JSON::ParserError => e
      # It's not valid json, show error GTFO..
      puts e.message
      puts e.backtrace
      return
    end

  end #end questions

end
